package com.zxp.common.core.validatecode.captcha;

import cn.hutool.captcha.AbstractCaptcha;
import com.wf.captcha.*;
import com.wf.captcha.base.Captcha;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

/**
 * 验证码生成工具
 *  
 * @Author: shollin
 * @Date: 2021年5月14日 上午9:23:15
 * @version:
 */
@Slf4j
@UtilityClass
public class CaptchaHelper {

	// 文档：https://gitee.com/whvse/EasyCaptcha
	public Captcha getEasyCaptcha(Integer width,Integer height, Integer len, Integer type) {
		
		if (width == null) {
			width = 80;
		}
		if (height == null) {
			height = 24;
		}
		boolean lenIsNull = false;
		if (len == null) {
			len = 4;
			lenIsNull = true;
		}
		
		Captcha captcha = null;
    	switch (type) {
			case 1:
				// png类型
				captcha = new SpecCaptcha(width,height,len);
				break;
			case 2:
				// gif类型
				captcha = new GifCaptcha(width,height,len);
				break;
			case 3:
				// 中文类型
				captcha = new ChineseCaptcha(width,height,len);
				break;
			case 4:
				//  中文gif类型
				captcha = new ChineseGifCaptcha(width,height,len);
				break;
			case 5:
				//  算术类型
				if(lenIsNull) {
					len = 2;
				}
				captcha = new ArithmeticCaptcha(width,height);
				captcha.setLen(len);  // 几位数运算，默认是两位
				// captcha.getArithmeticString() 获取运算的公式：3+2=?
				break;
			default:
				// png类型
				captcha = new SpecCaptcha(width,height,len);
		} 
    	
    	return captcha;
	}
	
	// 参考文档：https://www.hutool.cn/docs/#/captcha/%E6%A6%82%E8%BF%B0
	public AbstractCaptcha getCaptcha(Integer width,Integer height, Integer codeCount, Integer type,Integer interfereCount) {
		
		//定义图形验证码的长和宽
		if (width == null) {
			width = 80;
		}
		if (height == null) {
			height = 24;
		}
		
		
		AbstractCaptcha captcha = null;
		switch (type) {
			case 1:
				// Line类型, 默认干扰线150
				if (interfereCount == null) {
					interfereCount = 10;
				}
				captcha = cn.hutool.captcha.CaptchaUtil.createLineCaptcha(width, height,codeCount,interfereCount);
				break;
			case 2:
				// Circle类型  默认干扰线15
				if (interfereCount == null) {
					interfereCount = 15;
				}
				captcha = cn.hutool.captcha.CaptchaUtil.createCircleCaptcha(width, height,codeCount,interfereCount);
				break;
			case 3:
				// gif类型  默认干扰线10				
				captcha = cn.hutool.captcha.CaptchaUtil.createGifCaptcha(width, height, codeCount);
				break;
			case 4:
				//  Shear扭曲类型
				if (interfereCount == null) {
					interfereCount = 1;
				}
				captcha = cn.hutool.captcha.CaptchaUtil.createShearCaptcha(width, height,codeCount,interfereCount);
				break;
			
			default:
				// png类型
				captcha = cn.hutool.captcha.CaptchaUtil.createGifCaptcha(width, height, codeCount);
		} 
		
		return captcha;
	}
}
